/**
 * This file is part of the DbUnit module for NetBeans IDE
 * Copyright (C) 2009 Pavol Pitoňák
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package cz.muni.fi.dbunit;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.tree.TreeSelectionModel;
import org.openide.explorer.ExplorerManager;
import org.openide.nodes.Node;

/**
 *
 * @author Pavol Pitoňák <pavol@pitonak.com>
 */
public class DataSetNodePanel extends javax.swing.JPanel implements ExplorerManager.Provider {

    private ExplorerManager manager = new ExplorerManager();
    private PropertyChangeSupport pcs;

    /** Creates new form DataSetNodePanel */
    public DataSetNodePanel(Node root) {
        initComponents();

        beanTreeView.setRootVisible(false);
        beanTreeView.setDefaultActionAllowed(false);
        beanTreeView.setPopupAllowed(false);
        beanTreeView.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        manager.setRootContext(root);
        Node[] rootChildren = root.getChildren().getNodes();
        for (int i = 0; i < rootChildren.length; i++) {
            beanTreeView.expandNode(rootChildren[i]);
        }
        manager.addPropertyChangeListener(
                new PropertyChangeListener() {

                    public void propertyChange(PropertyChangeEvent pce) {
                        firePropertyChange();
                    }
                });

        setLayout(new BorderLayout());
        add(beanTreeView, BorderLayout.CENTER);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        beanTreeView = new org.openide.explorer.view.BeanTreeView();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(beanTreeView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(beanTreeView, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView;
    // End of variables declaration//GEN-END:variables

    public ExplorerManager getExplorerManager() {
        return manager;
    }

    private void firePropertyChange() {
        getProperChangeSupport().firePropertyChange(ExplorerManager.PROP_SELECTED_NODES, null, null);
    }

    private PropertyChangeSupport getProperChangeSupport() {
        if (pcs == null) {
            pcs = new PropertyChangeSupport(this);
        }
        return pcs;
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener l) {
        getProperChangeSupport().addPropertyChangeListener(l);
    }

    public String getFileName() {
        return buildFileName(manager.getSelectedNodes()[0]);
    }

    private String buildFileName(Node node) {
        if ("${src.dir}".equals(node.getName())) {
            return "src";
        }
        if ("${test.src.dir}".equals(node.getName())) {
            return "test";
        }
        // for Maven
        if ("1SourceRoot".equals(node.getName())) {
            return "src/main/java";
        }
        if ("2TestSourceRoot".equals(node.getName())) {
            return "test/java";
        }
        if ("OtherRoots".equals(node.getName())) {
            return "src/main";
        }

        if (node.isLeaf()) {
            return buildFileName(node.getParentNode()) + "/" + node.getDisplayName();
        } else {
            return buildFileName(node.getParentNode()) + "/" + node.getDisplayName().replaceAll("[.]", "/");
        }

    }

    public String getClassName() {
        return buildClassName(manager.getSelectedNodes()[0]).replace(".java", "");
    }

    private String buildClassName(Node node) {
        if (node.isLeaf()) {
            return buildClassName(node.getParentNode()) + "." + node.getDisplayName();
        } else {
            return node.getDisplayName();
        }
    }
    
    public Node[] getSelectedNodes() {
        return manager.getSelectedNodes();
    }
}
